package com.gsbelarus.gedemin.salary.fragment.workschedule;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.adapter.WScheduleRecylerViewAdapter;
import com.gsbelarus.gedemin.salary.entity.model.WorkScheduleModel;
import com.gsbelarus.gedemin.salary.ui.EmptyRecyclerView;

import java.text.SimpleDateFormat;

public class WorkScheduleDetailFragment extends Fragment {

    private WorkScheduleModel workScheduleModel;

    private WScheduleRecylerViewAdapter recyclerAdapter;

    public static WorkScheduleDetailFragment newInstance(WorkScheduleModel workScheduleModel) {
        Bundle args = new Bundle();
        args.putSerializable("calendarDay", workScheduleModel);

        WorkScheduleDetailFragment fragment = new WorkScheduleDetailFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        workScheduleModel = (WorkScheduleModel) getArguments().getSerializable("calendarDay");
        recyclerAdapter = new WScheduleRecylerViewAdapter(workScheduleModel.getEvents());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.work_schedule_detail_fragment, container, false);

        EmptyRecyclerView recyclerView = (EmptyRecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setAdapter(recyclerAdapter);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.scrollToPosition(0);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);
        recyclerView.setEmptyView(view.findViewById(R.id.empty_rec_view_schedule));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        }

        TextView tvDay = (TextView) view.findViewById(R.id.tvDay);
        TextView tvDayType = (TextView) view.findViewById(R.id.tvDayType);

        String day = new SimpleDateFormat("EEEE, d MMMM").format(workScheduleModel.getDate());
        tvDay.setText(day.substring(0, 1).toUpperCase() + day.substring(1).toLowerCase());

        String dayType = "";
        switch (workScheduleModel.getDayType()) {
            case WORK:
                dayType = getResources().getString(R.string.day_type_work).toUpperCase();
                break;
            case DAY_OFF:
                dayType = getResources().getString(R.string.day_type_day_off).toUpperCase();
                break;
        }
        tvDayType.setText(dayType);

        return view;
    }
}
